package com.dbh.alg.datastructure.binarytree;

import com.dbh.alg.datastructure.queue.TreeNode;
import com.dbh.alg.datastructure.stack.LinkedListStack;

import java.util.LinkedList;

/**
 * 二叉树前序遍历（值,左,右）
 */
public class E01Leetcode144 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(
            new TreeNode(new TreeNode(4), 2, null),
                1,
            new TreeNode(new TreeNode(5), 3, new TreeNode(6))
        );

        LinkedList<TreeNode> stack = new LinkedList<>();
        TreeNode curr = root;
        while (curr != null || !stack.isEmpty()) {
            if (curr != null) {
                colorPrintln("去:" + curr.val, 31);
                stack.push(curr);
                curr = curr.left;
            } else {
                TreeNode pop = stack.pop();
                colorPrintln("回:" + pop.val, 34);
                curr = pop.right;
            }
        }
    }

    private static void colorPrintln(String origin, int color) {
        System.out.printf("\033[%dm%s\033[0m%n", color, origin);
    }
}
